package com.bw.goods.mvvm.bind;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bw.goods.R;
import com.bw.goods.mvvm.adapter.HomeBnnerAdapter;
import com.bw.goods.mvvm.adapter.HomeHotAdapter;
import com.bw.goods.mvvm.adapter.HomeMenuAdapter;
import com.bw.goods.mvvm.adapter.HomeTextBannerAdapter;
import com.bw.goods.mvvm.adapter.HomeTopAdapter;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.goods.mvvm.model.entity.HomeMenuEntity;
import com.bw.goods.mvvm.widegt.Divder;
import com.bw.mvvm_common.BaseApplication;
import com.bw.skill.mvvm.view.SkillListActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author KK
 * @description:
 * @date :2021/10/14 18:02
 * 针对商品列表进行databinding自定义属性
 */
public class GoodsRvBind {

    @BindingAdapter(value = {"goodsadapter","imagebanner","txtbanner",
            "menudata","hotdata","topEntities","transform"})
    public static void bindGoodsRv(RecyclerView recyclerView,
                                   BaseQuickAdapter goodsadapter,
                                  ObservableField< List<HomeBannerEntity>> imagebanner,
                                   List<String> textBanners,
                                   List<HomeMenuEntity> menus,
                                   List<String> hotImage,
                                   List<HomeBannerEntity>topEntities,
                                   ViewPager.PageTransformer transformer ){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(),2);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new Divder(recyclerView.getContext()));

        View headView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.view_home_banner,null);
        //创建顶部Banner View
        Banner home_view_banner = headView.findViewById(R.id.home_view_banner);
        home_view_banner.setAdapter(new HomeBnnerAdapter(imagebanner.get()))
                .setOrientation(Banner.HORIZONTAL)
                .start();

        //创建文字Banner View
        Banner home_text_banner = headView.findViewById(R.id.home_text_banner);
        home_text_banner.setAdapter(new HomeTextBannerAdapter(textBanners))
                .setOrientation(Banner.VERTICAL)
                .start();
        //创建菜单
        RecyclerView home_menu_rv = headView.findViewById(R.id.home_menu_rv);
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),4);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        home_menu_rv.setLayoutManager(manager);
        HomeMenuAdapter homeMenuAdapter = new HomeMenuAdapter();
        homeMenuAdapter.setNewInstance(menus);
        home_menu_rv.setAdapter(homeMenuAdapter);
        homeMenuAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (position == 6) {
                        BaseApplication.getApplication().startActivity(new Intent(BaseApplication.getApplication(), SkillListActivity.class));
                }
            }
        });
        //创建热门商品水平recyclerview
        RecyclerView home_hot_rv = headView.findViewById(R.id.home_hot_rv);
        LinearLayoutManager hotManager = new LinearLayoutManager(recyclerView.getContext());
        hotManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        home_hot_rv.setLayoutManager(hotManager);
        HomeHotAdapter homeHotAdapter = new HomeHotAdapter();
        homeHotAdapter.setNewInstance(hotImage);
        home_hot_rv.setAdapter(homeHotAdapter);
        //创建画廊效果Viewpager
        ViewPager home_vp = headView.findViewById(R.id.home_vp);
        home_vp.setOffscreenPageLimit(4);
        home_vp.setPageTransformer(true,transformer);
        home_vp.setAdapter(new HomeTopAdapter(topEntities));

        goodsadapter.addHeaderView(headView);
        recyclerView.setAdapter(goodsadapter);



    }


    @BindingAdapter(value = "chanageadapter")
    public static void changebindRv(RecyclerView recyclerView, BaseQuickAdapter adapter){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(),3);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);

    }

    @BindingAdapter(value = "textadpter")
    public static void changeTextbindRv(RecyclerView recyclerView, BaseQuickAdapter adapter){
        LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
//        recyclerView.addItemDecoration(new Divder(recyclerView.getContext()));
        recyclerView.setAdapter(adapter);
    }

}
